
//每隔1分钟调用一次
// setInterval(function () {cust_type();},1000*60);

$(function(){

    //获取景区游玩类型
    getSecPlayTypelist();
	//获取农家游玩类型
    getfarmPlayTypelist();

    //下拉框
    $(document).on('click',".my_select_ul li",function(){
        $(this).parent().siblings("div").find("span").html($(this).text());
        $(this).parent().siblings("div").find("span").attr("value",$(this).attr("value"));
        if($(this).parent().siblings("div").find("span").hasClass("tourtype_select")){
            //挑选类型
            $(".farm").hide();
            $(".tour").hide();
            $(".pricebudget").hide();
            if($(this).attr("value") == "1"){
                //农家乐
                $(".pricebudget_text").text("人均预算");
                $(".farm").show();
                $(".pricebudget").show();
            }else if($(this).attr("value") == "2"){
                //景区
                $(".pricebudget_text").text("门票价格");
                $(".tour").show();
            }

        }else if($(this).parent().siblings("div").find("span").hasClass("ticket_select")){
            //门票
            $(".pricebudget").hide();
            if($(this).attr("value") == "0"){
                //不需要门票

            }else if($(this).attr("value") == "1"){
                //需要门票
                $(".pricebudget").show();
            }
        }
    })

    $(document).on("click",".sysremarkRemarks li a",function () {
        if($(this).hasClass("xinjian_btn_white")){
            $(this).removeClass("xinjian_btn_white");
            $(this).addClass("xinjian_btn");
        }else{
            $(this).addClass("xinjian_btn_white");
            $(this).removeClass("xinjian_btn");
        }
    })

    //搜索用户
    $("#sousuo_value").bind("input propertychange", function () {
        var filed = $(this).attr("name");
        var value = $(this).val().trim();
        getuserinfo(filed,value);
    });


})


// 用户搜索，判断用户是否存在
function getuserinfo(field,value) {
    var list = new Array();
    if (value != "") {
        $.post('/userquery', {'field':field, 'value': value}, function (value) {
            if (value.status == 0) {
                list = value.list;
                if(list.length>0){
                    var share = "";
                    var roleName = "";
                    for (var i = 0; i < list.length; i++) {
                        if(list[i].user_role == "0"){
                            roleName = "用户";
                        }else if(list[i].user_role == "1"){
                            roleName = "商家";
                        }
                        share += "<li onclick=\"selectPerson(this)\" value=\"" + list[i].user_id + "\" encryptId=\"" + list[i].user_encryptId + "\" tel=\"" + list[i].user_tel + "\" roleName=\"" +roleName + "\" email=\"" +list[i].mail + "\"><span class='show_tips'>" + list[i].user_realName + "</span></li>";
                    }
                    $("#username_ul").html(share);
                }
            } else {
                if(value=="" || value==null){
                    layer.alert("接口出现错误！", {icon: 2});
                }else {
                    layer.alert(value.msg, {icon: 2});
                }
            }
        });
    }
}
//搜索改变文本框的值
function selectPerson(info) {
    $("#share_studyconsultant").val($(info).text());
    $("#share_studyconsultant").attr("encryptId",$(info).attr("encryptId"));
    $("#share_studyconsultant").attr("tel",$(info).attr("tel"));
    $("#share_studyconsultant").attr("roleName",$(info).attr("roleName"));
    $("#share_studyconsultant").attr("email",$(info).attr("email"));
    $("#sousuo_value").val($(info).text());
    $("#tel").val($(info).attr("tel"));
    $("#email").val($(info).attr("email"));
}

//定制类型  （动态加载，通过请求等到数据）
function cust_type(){
    var typeName = $('#tourtype_select_name').attr("value");//选中的文本
    if(typeName == 1){
        $(".pricebudget_text").text("人均预算");
        $(".farm").show();
        $(".tour").hide();
    }else{
        $(".pricebudget_text").text("门票价格");
        $(".farm").hide();
        $(".tour").show();
    }
}

/**
 * 立即定制
 * @param user_encryptId 定制信息的用户加密id
 * @param type 定制类型，1：挑选农家，2：挑选景区
 * @param category 定制类型 1：私人定制，2：团队定制
 * @param days 游玩天数（1天0晚，1天1晚，2天1晚，2天2晚，3天2晚，4天以上）
 * @param roomtype 住宿类型(0：不住宿，1：经济型，2：豪华型)
 * @param diningl 用餐标准(0：自行安排，5:3-5人桌，8:5-8人桌，10:10-12人桌)
 * @param playTotalNum 游玩总人数
 * @param palyChildNum 游玩儿童人数
 * @param palyOldNum 游玩老人人数
 * @param min_pricebudget 最小人均预算
 * @param max_pricebudget 最大人均预算
 * @param departure 出发地点
 * @param destination 目的地，多个地区使用 ， 进行分隔
 * @param Wizard  向导需求(0：自行安排，1：领队，2：导游)
 * @param ticket 门票（1：需要，0：不需要）
 * @param departuretime 游玩时间
 * @param palydesc 游玩类型文字说明
 * @param playType_encryptId 游玩类型加密id
 * @param otherdemand 其他需求
 * @param username 用户名
 * @param user_tel 联系电话
 * @param useremail 邮箱
 * @param dataSources 数据来源（0：后台管理系统，1：前端官网）
 */
function custom(){
    var type = $("#tourtype_select_name").attr("value");
    var category = $("#wizard option:selected").attr("value");
    var days = $("#days option:selected").attr("value");
    var roomtype = $("#roomtype option:selected").attr("value");
    var diningl = $("#diningl option:selected").attr("value");
    var playTotalNum = $("#playTotalNum").val().trim();
    var palyChildNum = $("#palyChildNum").val().trim();
    var palyOldNum = $("#palyOldNum").val().trim();

    var departure = $("#departure").val().trim();
    var destination = $("#destination").val().trim();
    var Wizard = $("#wizard_ option:selected").attr("value");

    var min_pricebudget = $("#min_pricebudget").val().trim();
    var max_pricebudget = $("#max_pricebudget").val().trim();
    var ticket = 0; //农家乐无需门票
    if(type == "2"){
        //景区
        ticket = $("#ticket_select_name").attr("value");
    }
    var departuretime = $("#start").val().trim();

    var playTypeIdArray = new Array(); //游玩类型id
    var palydesc = new Array(); //游玩类型
    if(type == "1"){
        //挑选农家
        $("#farm_playtype").find(".xinjian_btn").each(function () {
            if ($.inArray($(this).attr("value"), playTypeIdArray) == -1) {
                playTypeIdArray.push($(this).attr("value"));
                palydesc.push($(this).attr("text"));
            }
        });
    }else if(type == "2"){
        //挑选景区
        $("#tour_playtype").find(".xinjian_btn").each(function () {
            if ($.inArray($(this).attr("value"), playTypeIdArray) == -1) {
                playTypeIdArray.push($(this).attr("value"));
                palydesc.push($(this).attr("text"));
            }
        });
    }
    var otherdemand = $("#remarks").val().trim();
    var username = $("#share_studyconsultant").attr("value");
    var user_encryptId = $("#share_studyconsultant").attr("encryptid");
    var user_tel = $("#tel").val().trim();
    var useremail = $("#email").val().trim();
    var dataSources = "0";
    if(playTotalNum == ""){
        //因为数据库存储类型为int
        playTotalNum = 0;
    }
    if(palyChildNum == ""){
        palyChildNum = 0;
    }
    if(palyOldNum == ""){
        palyOldNum = 0;
    }
    if(ticket == "0" && type == "2"){
        //不需要门票（景区）
        min_pricebudget = 0;
        max_pricebudget = 0;
    }

    var custjson={
        "type":type,
        "category":category,
        "days":days,
        "roomtype":roomtype,
        "diningl":diningl,
        "playTotalNum":playTotalNum,
        "palyChildNum":palyChildNum,
        "palyOldNum":palyOldNum,
        "min_pricebudget":min_pricebudget,
        "max_pricebudget":max_pricebudget,
        "departure":departure,
        "destination":destination,
        "Wizard":Wizard,
        "ticket":ticket,
        "departuretime":departuretime,
        "playType_encryptId":playTypeIdArray.join(","),
        "palydesc":palydesc.join(","),
        "otherdemand":otherdemand,
        "username":username,
        "user_encryptId":user_encryptId,
        "user_tel":user_tel,
        "useremail":useremail,
        "dataSources":dataSources
    }
    var reg = /^1[0|1|2|3|4|5|6|7|8|9]\d{9}$/;//电话验证
    if(user_encryptId == "" && user_encryptId == undefined && user_encryptId == "undefined"){
        layer.msg("请选择联系人！",{icon:0,time:2000});
    }else if(user_tel == ""){
        layer.msg("请填写手机号码！",{icon:0,time:2000});
    }else if(user_tel.length != 11 || !reg.test(user_tel)){
        layer.msg("请输入正确格式的手机号码！",{icon:0,time:2000});
    }else if(useremail == ""){
        layer.msg("留下你的邮箱方便联系您！",{icon:0,time:2000});
    }else {
        var ii = layer.load(2);
        $.post('/custom/addcustom', {"custjson":JSON.stringify(custjson)}, function (data) {
            layer.close(ii);
            if (data.status == 0 && data.msg == 'ok') {

                $(".clearaway").val("");
                $("#sousuo_value").val("");
                $("#share_studyconsultant").attr("value");
                $("#share_studyconsultant").attr("encryptid");
                $("#share_studyconsultant").attr("tel");
                $("#share_studyconsultant").attr("tel");
                $("#share_studyconsultant").attr("rolename");
                $("#share_studyconsultant").attr("email");
                //清空样式
                $(".sysremarkRemarks li a").find(".xinjian_btn").each(function () {
                    $(this).addClass("xinjian_btn_white ");
                    $(this).removeClass("xinjian_btn");
                });

                setcookie("cust_encryptid",data.cust_Encryptid,1,"/");//定制加密id
                setcookie("user_encryptid",data.user_Encryptid,1,"/");//用户加密id
                setcookie("user_id",data.user_id,1,"/");//用户id
                setcookie("tel",data.tel,1,"/");//用户电话号码
                var size = "pick-farm/pick_info_new.html".length;
                window.parent.addTab_father("定制详情", "pick-farm/pick_info_new.html", size);
            } else {
                if(data.msg==null||data.msg==""){
                    layer.alert("接口出现错误！", {icon: 2})
                }else{
                    if(data.status == 40004){
                        layer.alert(data.msg, {icon: 0})
                    }else {
                        layer.alert(data.msg, {icon: 2})
                    }
                }
            }
        })
    }
}

/****************************************------公共js--------*****************************************************/

//获取农家乐游玩类型
var allfarmPlaytypelist = new Array();
function getfarmPlayTypelist() {
    $("#farm_playtype").html("");
    $.post('/farmplaytype/getAllfarmPlaytype', {}, function (data) {
        if (data.status == 0 && data.msg == 'ok') {
            var list = data.list;
            allfarmPlaytypelist = data.list;
            var str = "";
            if(list.length>0){
                for(var i=0;i<list.length;i++){
                    str += "<li><a href='javascript:void(0)' class='xinjian_btn_white remarks_a' name='"+list[i].farmId+"' value='"+list[i].encryptId+"' text='"+list[i].title+"'>" +list[i].title+ "</a></li>";
                }
                $("#farm_playtype").html(str);
            }
        } else {
            if(data.msg==null||data.msg==""){
                layer.alert("接口出现错误！", {icon: 2})
            }else{
                layer.alert(data.msg, {icon: 2})
            }
        }
    })
}

//获取景区游玩类型
var allScePlaytypelist = new Array();
function getSecPlayTypelist() {
    $("#tour_playtype").html("");
    $.post('/ScePlayType/getAllScePlaytype', {}, function (data) {
        if (data.status == 0 && data.msg == 'ok') {
            var list = data.list;
            allScePlaytypelist = data.list;
            var str = "";
            if(list.length>0){
                for(var i=0;i<list.length;i++){
                	str += "<li><a href='javascript:void(0)' class='xinjian_btn_white remarks_a' name='"+list[i].sceplayId+"' value='"+list[i].encryptId+"' text='"+list[i].title+"'>" +list[i].title+ "</a></li>";
                }
                $("#tour_playtype").html(str);
            }
        } else {
            if(data.msg==null||data.msg==""){
                layer.alert("接口出现错误！", {icon: 2})
            }else{
                layer.alert(data.msg, {icon: 2})
            }
        }
    })
}
